package com.well.module.system.web;

import com.github.pagehelper.PageInfo;
import com.well.kernel.spring.web.ResponseData;
import com.well.kernel.spring.web.easyui.Combobox;
import com.well.kernel.spring.web.easyui.Datagrid;
import com.well.module.system.entity.Dictionary;
import com.well.module.system.service.DictionaryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/dictionary")
public class DictionaryController{

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 进入首页页面
     * @param modelMap
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView index(ModelMap modelMap){
        // TODO:请输入页面
        return new ModelAndView("",modelMap);
    }

    /**
     * 获取 easyui 的 datagrid 分页数据
     * @param dictionary
     * @return
     */
    @RequestMapping("/datagrid")
    @ResponseBody
    public Datagrid<Dictionary> datagrid(Dictionary dictionary){
        PageInfo<Dictionary> pageInfo=dictionaryService.selectPageInfo(dictionary);
        Datagrid<Dictionary> datagrid=new Datagrid<Dictionary>(pageInfo);
        return datagrid;
    }

    @RequestMapping("/selectList")
    @ResponseBody
    public List<Dictionary> selectList(Dictionary dictionary){
        return dictionaryService.selectList(dictionary);
    }

    /**
     * 进入详情页面
     * @param id 主键
     * @param modelMap
     * @return
     */
    @RequestMapping("/detail")
    public ModelAndView detail(String id,ModelMap modelMap){
        Dictionary dictionary=dictionaryService.select(id);
        modelMap.put("dictionary",dictionary);
        // TODO:请输入页面
        return new ModelAndView("",modelMap);
    }

    /**
     * 保存
     * @param dictionary
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResponseData save(Dictionary dictionary){
        int result=dictionaryService.save(dictionary);
        if(result==0){
            return new ResponseData(false,"保存失败！");
        }
        return new ResponseData(true,"保存成功！");
    }

    /**
     * 逻辑删除
     * @param ids 多个主键，以","分割
     * @return
     */
    @RequestMapping("/logicalDelete")
    @ResponseBody
    public ResponseData logicalDelete(String ids){
        int result=dictionaryService.logicalDelete(ids);
        if(result==0){
            return new ResponseData(false,"删除失败！");
        }
        return new ResponseData(true,"删除【"+result+"】条数据成功！");
    }

}
